﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zocono.WCS.Domain.PlcBytesObject
{
    public class ElevatorGeneralPlcActionBO : BytesObjectBase
    {
        #region 指令信息
        /// <summary>
        /// LIFT自动中
        /// </summary>
        public bool Auto { get; set; }
        /// <summary>
        /// Busy
        /// </summary>
        public bool Busy { get; set; }
        /// <summary>
        /// 上层有盘
        /// </summary>
        public bool UpMaterial { get; set; }
        /// <summary>
        /// 下层有盘
        /// </summary>
        public bool DownMaterial { get; set; }
        /// <summary>
        /// 任务完成
        /// </summary>
        public bool TaskComplete { get; set; }
        /// <summary>
        /// 报警
        /// </summary>
        public bool LiftAlrm { get; set; }
        /// <summary>
        /// 模拟自动
        /// </summary>
        public bool Analogautomatic { get; set; }
        /// <summary>
        /// 报警代码
        /// </summary>
        public short LiftAlrmCode { get; set; }
        /// <summary>
        /// 当前楼
        /// </summary>
        public short CurrentFloor { get; set; }
        /// <summary>
        /// 当前层
        /// </summary>
        public short CurrentLayer { get; set; }
        /// <summary>
        /// 当前上层功能
        /// </summary>
        public short CurrentUpFunction { get; set; }
        /// <summary>
        /// 当前下层功能
        /// </summary>
        public short CurrentDownFunction { get; set; }

        #endregion

        #region 上层
        public string UpEquipmentCode { get; set; }
        public int UpTaskCode { get; set; }
        public int UpToEquipmentCode { get; set; }
        public string UpPalletBarcode { get; set; }
        public short UpActionType { get; set; }
        public byte UpPalletType { get; set; }
        public byte UpAlrmCode { get; set; }
        /// <summary>
        /// 预留
        /// </summary>
        public byte UpReserve { get; set; }
        public byte UpReserve2 { get; set; }
        public byte UpReserve3 { get; set; }
        public byte UpReserve4 { get; set; }

        #endregion

        #region 下层
        public string DownEquipmentCode { get; set; }
        public int DownTaskCode { get; set; }
        public int DownToEquipmentCode { get; set; }
        public string DownPalletBarcode { get; set; }
        public short DownActionType { get; set; }
        public byte DownPalletType { get; set; }
        public byte DownAlrmCode { get; set; }
        /// <summary>
        /// 预留
        /// </summary>
        public byte DownReserve { get; set; }
        public byte DownReserve2 { get; set; }
        public byte DownReserve3 { get; set; }
        public byte DownReserve4 { get; set; }

        #endregion

        #region 报警详情
        /// <summary>
        /// 急停报警
        /// </summary>
        public bool StopAlrm { get; set; }
        /// <summary>
        /// 上限位报警
        /// </summary>
        public bool UpLimitAlrm { get; set; }
        /// <summary>
        /// 下限位报警
        /// </summary>
        public bool DownLimitAlrm { get; set; }
        /// <summary>
        /// 上层左限位报警
        /// </summary>
        public bool UpLeftLimitAlrm { get; set; }
        /// <summary>
        /// 上层右限位报警
        /// </summary>
        public bool UpRightLimitAlrm { get; set; }
        /// <summary>
        /// 下层左限位报警
        /// </summary>
        public bool DownLeftLimitAlrm { get; set; }
        /// <summary>
        /// 下层右限位报警
        /// </summary>
        public bool DownRightLimitAlrm { get; set; }
        /// <summary>
        /// 变频器报错
        /// </summary>
        public bool InverterAlrm { get; set; }
        /// <summary>
        /// WCS楼层信息发送错误报警
        /// </summary>
        public bool WCSLayerSendAlrm { get; set; }
        /// <summary>
        /// HMI楼层信息发送错误报警
        /// </summary>
        public bool HMILayerSendAlrm { get; set; }
        /// <summary>
        /// 上层输送电机取料超时报警
        /// </summary>
        public bool UpTakeMaterialOutTime { get; set; }
        /// <summary>
        /// 下层输送电机取料超时报警
        /// </summary>
        public bool DownTakeMaterialOutTime { get; set; }
        /// <summary>
        /// 上层输送电机取料超时报警
        /// </summary>
        public bool UpPutMaterialOutTime { get; set; }
        /// <summary>
        /// 下层输送电机取料超时报警
        /// </summary>
        public bool DownPutMaterialOutTime { get; set; }
        /// <summary>
        /// 定位位置错误报警
        /// </summary>
        public bool LocationAlrm { get; set; }
        /// <summary>
        /// 定位到达位置与实际位置不符报警
        /// </summary>
        public bool ArriveLocationAlrm { get; set; }
        /// <summary>
        /// LIFT运行中托盘撞到上层左极限报警
        /// </summary>
        public bool BumpUpLeftLimitAlrm { get; set; }
        /// <summary>
        /// LIFT运行中托盘撞到上层右极限报警
        /// </summary>
        public bool BumpUpRightLimitAlrm { get; set; }
        /// <summary>
        /// LIFT运行中托盘撞到下层左极限报警
        /// </summary>
        public bool BumpDownLeftLimitAlrm { get; set; }
        /// <summary>
        /// LIFT运行中托盘撞到下层右极限报警
        /// </summary>
        public bool BumpDownRightLimitAlrm { get; set; }

        #endregion
        public override void Serialize(long plcConnectionID, long plcReadDbID, int bytesIndex, int bytesLength, int objectIndex, int objectLength, string objectType, string ScEquipmentCode, byte[] bytes)
        {
            PlcConnectionID = plcConnectionID;
            PlcReadDbID = plcReadDbID;
            BytesIndex = bytesIndex;
            BytesLength = bytesLength;
            ObjectIndex = objectIndex;
            ObjectLength = objectLength;
            ObjectType = objectType;


            EquipmentCode = ScEquipmentCode;
            Auto = HelperByte.GetBitAt(bytes, 0, 0);
            Busy = HelperByte.GetBitAt(bytes, 0, 1);
            UpMaterial = HelperByte.GetBitAt(bytes, 0, 2);
            DownMaterial = HelperByte.GetBitAt(bytes, 0, 3);
            TaskComplete = HelperByte.GetBitAt(bytes, 0, 4);
            LiftAlrm = HelperByte.GetBitAt(bytes, 0, 5);
            Analogautomatic = HelperByte.GetBitAt(bytes, 0, 6);
            LiftAlrmCode = HelperByte.GetIntAt(bytes, 2);
            CurrentFloor = HelperByte.GetIntAt(bytes, 4);
            CurrentLayer = HelperByte.GetIntAt(bytes, 6);
            CurrentUpFunction = HelperByte.GetIntAt(bytes, 8);
            CurrentDownFunction = HelperByte.GetIntAt(bytes, 10);


            UpEquipmentCode = HelperByte.GetDIntAt(bytes, 12).ToString();
            UpTaskCode = HelperByte.GetDIntAt(bytes, 16);
            UpToEquipmentCode = HelperByte.GetDIntAt(bytes, 20);
            int lengthUp = HelperByte.GetSIntAt(bytes, 25);
            UpPalletBarcode = HelperByte.GetCharsAt(bytes, 26, lengthUp);
            UpActionType = HelperByte.GetIntAt(bytes, 44);
            UpPalletType = HelperByte.GetByteAt(bytes, 46);
            UpAlrmCode = HelperByte.GetByteAt(bytes, 47);
            UpReserve = HelperByte.GetByteAt(bytes, 48);
            UpReserve2 = HelperByte.GetByteAt(bytes, 49);
            UpReserve3 = HelperByte.GetByteAt(bytes, 50);
            UpReserve4 = HelperByte.GetByteAt(bytes, 51);

            DownEquipmentCode = HelperByte.GetDIntAt(bytes, 52).ToString();
            DownTaskCode = HelperByte.GetDIntAt(bytes, 56);
            DownToEquipmentCode = HelperByte.GetDIntAt(bytes, 60);
            int lengthDown = HelperByte.GetSIntAt(bytes, 65);
            DownPalletBarcode = HelperByte.GetCharsAt(bytes, 66, lengthDown);
            DownActionType = HelperByte.GetIntAt(bytes, 84);
            DownPalletType = HelperByte.GetByteAt(bytes, 86);
            DownAlrmCode = HelperByte.GetByteAt(bytes, 87);
            DownReserve = HelperByte.GetByteAt(bytes, 88);
            DownReserve2 = HelperByte.GetByteAt(bytes, 89);
            DownReserve3 = HelperByte.GetByteAt(bytes, 90);
            DownReserve4 = HelperByte.GetByteAt(bytes, 91);

            StopAlrm = HelperByte.GetBitAt(bytes, 92, 0);
            UpLimitAlrm = HelperByte.GetBitAt(bytes, 92, 1);
            DownLimitAlrm = HelperByte.GetBitAt(bytes, 92, 2);
            UpLeftLimitAlrm = HelperByte.GetBitAt(bytes, 92, 3);
            UpRightLimitAlrm = HelperByte.GetBitAt(bytes, 92, 4);
            DownLeftLimitAlrm = HelperByte.GetBitAt(bytes, 92, 5);
            DownRightLimitAlrm = HelperByte.GetBitAt(bytes, 92, 6);
            InverterAlrm = HelperByte.GetBitAt(bytes, 92, 7);
            WCSLayerSendAlrm = HelperByte.GetBitAt(bytes, 93, 0);
            HMILayerSendAlrm = HelperByte.GetBitAt(bytes, 93, 1);
            UpTakeMaterialOutTime = HelperByte.GetBitAt(bytes, 93, 2);
            DownTakeMaterialOutTime = HelperByte.GetBitAt(bytes, 93, 3);
            UpPutMaterialOutTime = HelperByte.GetBitAt(bytes, 93, 4);
            DownPutMaterialOutTime = HelperByte.GetBitAt(bytes, 93, 5);
            LocationAlrm = HelperByte.GetBitAt(bytes, 93, 6);
            ArriveLocationAlrm = HelperByte.GetBitAt(bytes, 93, 7);
            BumpUpLeftLimitAlrm = HelperByte.GetBitAt(bytes, 94, 0);
            BumpUpRightLimitAlrm = HelperByte.GetBitAt(bytes, 94, 1);
            BumpDownLeftLimitAlrm = HelperByte.GetBitAt(bytes, 94, 2);
            BumpDownRightLimitAlrm = HelperByte.GetBitAt(bytes, 94, 3);

        }

    }
}
